/*
 * @Author: wuzhouhai wzh200x@126.com
 * @Date: 2024-07-30 16:57:05
 * @LastEditors: wuzhouhai wzh200x@126.com
 * @LastEditTime: 2024-08-07 10:36:51
 * @FilePath: \dwys_web\assets\script\ui\scrollViewGameMsg\ScrollViewGameMsg.ts
 * @Description: 
 */
import { _decorator, Component, Node } from 'cc';
import { CCComp } from '../../../../extensions/oops-plugin-framework/assets/module/common/CCComp';
import { ScrollView } from 'cc';
import { Prefab } from 'cc';
import { oops } from '../../../../extensions/oops-plugin-framework/assets/core/Oops';
import { GameEvent } from '../../common/event/GameEvent';
import { instantiate } from 'cc';
import { ItemMsg } from '../itemMsg/ItemMsg';
const { ccclass, property } = _decorator;

@ccclass('ScrollViewGameMsg')
export class ScrollViewGameMsg extends CCComp {
    @property(Prefab)
    itemMsg: Prefab|null = null;

    reset(): void {
        this.node.getComponent(ScrollView)!.content?.removeAllChildren();
        this.node.getComponent(ScrollView)!.scrollToTop(0);
    }

    start() {
        oops.message.on(GameEvent.AddGameMsgInfo, this.onAddMsgInfo, this);
    }

    protected onDestroy(): void {
        oops.message.off(GameEvent.AddGameMsgInfo, this.onAddMsgInfo, this);
    }

    update(deltaTime: number) {
        
    }

    onAddMsgInfo(event:string, msg:any){
        // 创建itemMsg预制体
        let itemMsgNode = instantiate(this.itemMsg!);
        let itemMsg:ItemMsg = itemMsgNode.getComponent(ItemMsg)!;
        itemMsg.UpdateLabel(msg);
        // 判断是否大于50条信息,大于就删除第最后条,并从ScrollView删除节点
        if(this.node.getComponent(ScrollView)!.content!.children.length > 20){
            this.node.getComponent(ScrollView)!.content!.children.pop()?.destroy();
        }
        this.node.getComponent(ScrollView)!.content!.insertChild(itemMsgNode, 0);
    }
}


